package servlet;

import bean.Category;
import org.apache.commons.fileupload.FileUploadException;
import util.ImageUtil;
import util.Page;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CategoryServlet extends BaseBackServlet {
    @Override
    public String add(HttpServletRequest request, HttpServletResponse response, Page page) {
        Map<String, String> map = new HashMap<>();
        try {
            InputStream in = ImageUtil.upload(request, map);
//            System.out.println(in);
            Category category = new Category();
            String name = map.get("category_name");
            category.setName(name);
            categoryDAO.add(category);
//            System.out.println(category);
            int id = category.getId();
            ServletContext sc = request.getServletContext();
            String productsPath = sc.getRealPath("img/category");
            File desc = new File(productsPath);
            if (!desc.exists()) {
                desc.mkdirs();
            }
            String fileName = id + ".jpg";
            File file = new File(desc, fileName);
            FileOutputStream out = new FileOutputStream(file);
            ImageUtil.copyFile(in, out);
            BufferedImage img = ImageUtil.change2jpg(file);
            ImageIO.write(img, "jpg", file);
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "@admin_category_list";
    }
        @Override
    public String delete(HttpServletRequest request, HttpServletResponse respone, Page page)
        {
            String str = "";
            int id = Integer.parseInt(request.getParameter("cid"));
            Category category = categoryDAO.get(id);
            if(category.getProducts()!=null)
            {
                str = "@admin_category_list?delete=no";
            }
            else
            {
                categoryDAO.delete(id);
                String path = request.getServletContext().getRealPath("img/category");
                ImageUtil.delFile(path,id);
                str = "@admin_category_list";
            }
            return str;
        }

    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, Page page)
    {
        int id = Integer.parseInt(request.getParameter("cid"));
        Category category = categoryDAO.get(id);
        request.setAttribute("category",category);
        return "jsp/admin/editCategory.jsp";
    }

    @Override
    public String update(HttpServletRequest request, HttpServletResponse response, Page page)
    {
        Map<String, String> map = new HashMap<>();
        InputStream in = null;
        FileOutputStream out = null;
        try {
            in = ImageUtil.upload(request, map);
            Category category = new Category();
            String name = map.get("category_name");
            int id  = Integer.parseInt(map.get("category_id"));
            category.setName(name);
            category.setId(id);
            categoryDAO.update(category);
            ServletContext sc = request.getServletContext();
            String productsPath = sc.getRealPath("img/category");
            File desc = new File(productsPath);
            if (!desc.exists()) {
                desc.mkdirs();
            }
            String fileName = id + ".jpg";
            File file = new File(desc, fileName);
            out = new FileOutputStream(file);
            ImageUtil.copyFile(in, out);
            BufferedImage img = ImageUtil.change2jpg(file);
            ImageIO.write(img, "jpg", file);
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "@admin_category_list";
    }

    @Override
    public String list(HttpServletRequest request, HttpServletResponse response, Page page) {
        int start = page.getStart();
        int count = page.getCount();
        int total = categoryDAO.getTotal();
//        System.out.println(start+"--"+count+"--"+total);
        page.setTotal(total);
        List<Category> categories = categoryDAO.list(start, count);
        request.setAttribute("categories", categories);
        request.setAttribute("page", page);
        return "jsp/admin/listCategory.jsp";
    }
}
